package com.tyx.chargingpile.service.impl;

import com.tyx.chargingpile.Enum.ServiceMessageEume;
import com.tyx.chargingpile.cache.SocketChannelMap;
import com.tyx.chargingpile.controller.ChargingPileController;
import com.tyx.chargingpile.pojo.ChargingStartEndRecord;
import com.tyx.chargingpile.service.ChargingPileService;
import com.tyx.chargingpile.service.ChargingStartEndRecordService;
import com.tyx.chargingpile.service.MessageService;
import com.tyx.chargingpile.util.Util;
import com.tyx.chargingpile.vo.SocketResponseResult;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * d电桩开启充电下行
 */
@Service("chargingPileOpenDown")
public class ChargingPileOpenDown implements ChargingPileService {

    @Autowired
    private ChargingStartEndRecordService chargingStartEndRecordService;

    private final static Logger LOGGER = LoggerFactory.getLogger(ChargingPileOpenDown.class);

    @Override
    public SocketResponseResult execute(ChannelHandlerContext channelHandlerContext, byte[] context) throws Exception {
        return null;
    }

    @Override
    public SocketResponseResult execute(ChannelHandlerContext channelHandlerContext, Map data) throws Exception {
        Map<String,String> dwn = new HashMap<>();
        String pileNum = (String) data.get("PileId"); // 桩编号
        String gunpointNum = (String) data.get("ChargePortIndex"); //枪号
        String orderNum = (String) data.get("Orderid"); // 订单编号
        String chargeModel = (String) data.get("ChargeMode"); // 充电模式
        String chargeModelParam = (String) data.get("ChargeLimit"); // 充电模式参数
        String accountMoney = (String) data.get("ChargeMoney"); // 充电金额，单位元
        String powerType = SocketChannelMap.getPileInfo(pileNum).getPowerType();

        // 插入数据库，登记开启记录
        ChargingStartEndRecord chargingStartEndRecord = new ChargingStartEndRecord();
        chargingStartEndRecord.setGunpointNum(gunpointNum);
        chargingStartEndRecord.setPileNum(pileNum);
        chargingStartEndRecord.setOrderNum(orderNum);
        chargingStartEndRecord.setChargeMode(chargeModel);
        chargingStartEndRecord.setChargeModeParam(chargeModelParam);
        chargingStartEndRecord.setChargeMoney(accountMoney);
        chargingStartEndRecord.setPowerType(powerType);
        chargingStartEndRecordService.add(chargingStartEndRecord);

        // 对数据长度不够的，前补0
        pileNum = Util.FronFillZero(pileNum,12);
        gunpointNum = Util.FronFillZero(gunpointNum,2);
        chargeModel = Util.FronFillZero(chargeModel,2);
        accountMoney = Util.formatDoubleTOInt(Double.parseDouble(accountMoney),100) + ""; // 转化为分
        accountMoney = Util.FronFillZero(accountMoney,6);
        chargeModelParam = Util.formatDoubleTOInt(Double.parseDouble(chargeModelParam),100) + ""; // 转化为分
        chargeModelParam = Util.FronFillZero(chargeModelParam,6);
        // 组装下发报文
        dwn.put("gunpointNum",gunpointNum);
        dwn.put("powerType",powerType);
        dwn.put("pileNum",pileNum);
        dwn.put("orderNum",orderNum);
        dwn.put("chargeModel",chargeModel);
        dwn.put("chargeModelParam",chargeModelParam);
        dwn.put("accountMoney",accountMoney);
        byte[] dwnContext = MessageService.getMessage(dwn, ServiceMessageEume.OPEND.getDwnLinePath());
        ByteBuf pingMessage = Unpooled.buffer();
        pingMessage.writeBytes(dwnContext);
        channelHandlerContext.writeAndFlush(pingMessage);
        String commandId = ServiceMessageEume.OPEND.getCode();
        LOGGER.info("客户端"+channelHandlerContext.channel().id().toString()+",指令控制编号："+commandId+",桩编号:" + pileNum +  ",订单编号--" + orderNum + ",前置下发电桩启动充电下行报文"+ Util.BCD2String(dwnContext));
        return new SocketResponseResult(channelHandlerContext,dwnContext);
    }
}
